- name: verify the backend, and stop it
  hosts: pdc-backend:pdc-backend-stg
  user: root
  vars_files: 
   - /srv/web/infra/ansible/vars/global.yml
   - "/srv/private/ansible/vars.yml"
   - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  handlers:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"

  pre_tasks:
  - name: clean all metadata
    command: yum clean all
    check_mode: no
  - name: yum update PDC packages
    yum: name="{{item}}" state=latest
    with_items:
    - pdc-client
    - pdc-updater
    - python-productmd

  roles:
  - pdc/backend

  post_tasks:
  - name: tell nagios to shush w.r.t. the backend
    nagios: action=downtime minutes=15 service=host host={{ inventory_hostname_short }}{{ env_suffix }}
    delegate_to: noc01.phx2.fedoraproject.org
    ignore_errors: true
  - name: Stop the pdc-updater backend
    service: name="fedmsg-hub" state=stopped

- name: verify the frontend, stop it, upgrade the db, and restart it
  hosts: pdc-web:pdc-web-stg
  user: root
  vars_files: 
   - /srv/web/infra/ansible/vars/global.yml
   - "/srv/private/ansible/vars.yml"
   - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  handlers:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"

  pre_tasks:
  - name: clean all metadata
    command: dnf clean all
    check_mode: no
  - name: dnf update PDC packages
    dnf: name="{{item}}" state=latest
    with_items:
    - python-pdc
    - python-productmd

  roles:
  - pdc/frontend

  post_tasks:
  - name: tell nagios to shush w.r.t. the frontend
    nagios: action=downtime minutes=15 service=host host={{ inventory_hostname_short }}{{ env_suffix }}
    delegate_to: noc01.phx2.fedoraproject.org
    ignore_errors: true
  - service: name="httpd" state=stopped
  - name: Upgrade the database, only on the primary web node.
    command: python /usr/lib/python2.7/site-packages/pdc/manage.py migrate --noinput
    args:
        chdir: /usr/lib/python2.7/site-packages/pdc/
    when: inventory_hostname_short == 'pdc-web01'
  - service: name="httpd" state=started
  - name: tell nagios to unshush w.r.t. the frontend
    nagios: action=unsilence service=host host={{ inventory_hostname_short }}{{ env_suffix }}
    delegate_to: noc01.phx2.fedoraproject.org
    ignore_errors: true

- name: restart the backend
  hosts: pdc-backend:pdc-backend-stg
  user: root
  vars_files: 
   - /srv/web/infra/ansible/vars/global.yml
   - "/srv/private/ansible/vars.yml"
   - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
  handlers:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"

  tasks:
  - name: And... start the backend again
    service: name="fedmsg-hub" state=started
  - name: tell nagios to unshush w.r.t. the backend
    nagios: action=unsilence service=host host={{ inventory_hostname_short }}{{ env_suffix }}
    delegate_to: noc01.phx2.fedoraproject.org
    ignore_errors: true
